
using System;
using System.Web.UI;
//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using DotNetNuke.Common;
using DotNetNuke.Security;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.WebControls;

namespace DotNetNuke.Modules.Html
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The HtmlModule Class provides the UI for displaying the Html
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    public partial class HtmlModule : Entities.Modules.PortalModuleBase, Entities.Modules.IActionable
    {

        private int WorkflowID;

        private bool EditorEnabled;
        #region "Private Methods"

        #endregion

        #region "Event Handlers"

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Init runs when the control is initialized
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
			lblContent.UpdateLabel += new DNNLabelEdit.DNNLabelEditEventHandler(lblContent_UpdateLabel);
            EditorEnabled = PortalSettings.InlineEditorEnabled;
            try
            {
                WorkflowID = new HtmlTextController().GetWorkflow(ModuleId, TabId, PortalId).Value;

                //Add an Action Event Handler to the Skin
                AddActionHandler(ModuleAction_Click);
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                HtmlTextController objHTML = new HtmlTextController();
                WorkflowStateController objWorkflow = new WorkflowStateController();

                // edit in place
                if (EditorEnabled == true && this.IsEditable == true && PortalSettings.UserMode == DotNetNuke.Entities.Portals.PortalSettings.Mode.Edit)
                {
                    EditorEnabled = true;
                }
                else
                {
                    EditorEnabled = false;
                }

                // get content
                HtmlTextInfo objContent = null;
                string strContent = "";

                objContent = objHTML.GetTopHtmlText(ModuleId, !this.IsEditable, WorkflowID);

                if ((objContent != null))
                {
                    //don't decode yet (this is done in FormatHtmlText)
                    strContent = objContent.Content;
                }
                else
                {
                    // get default content from resource file
                    if (PortalSettings.UserMode == DotNetNuke.Entities.Portals.PortalSettings.Mode.Edit)
                    {
                        if (EditorEnabled)
                        {
                            strContent = Localization.GetString("AddContentFromToolBar.Text", LocalResourceFile);
                        }
                        else
                        {
                            strContent = Localization.GetString("AddContentFromActionMenu.Text", LocalResourceFile);
                        }
                    }
                    else
                    {
                        // hide the module if no content and in view mode
                        this.ContainerControl.Visible = false;
                    }
                }

                // token replace
                if (EditorEnabled && Settings["HtmlText_ReplaceTokens"] != null)
                {
                    EditorEnabled = !Convert.ToBoolean(Settings["HtmlText_ReplaceTokens"]);
                }

                // localize toolbar
                if (EditorEnabled)
                {
                    foreach (DotNetNuke.UI.WebControls.DNNToolBarButton objButton in this.tbEIPHTML.Buttons)
                    {
                        objButton.ToolTip = Services.Localization.Localization.GetString("cmd" + objButton.ToolTip, LocalResourceFile);
                    }
                }
                else
                {
                    this.tbEIPHTML.Visible = false;
                }

                lblContent.EditEnabled = EditorEnabled;

                // add content to module
                lblContent.Controls.Add(new LiteralControl(HtmlTextController.FormatHtmlText(ModuleId, strContent, Settings)));

            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// lblContent_UpdateLabel allows for inline editing of content
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        private void lblContent_UpdateLabel(object source, UI.WebControls.DNNLabelEditEventArgs e)
        {
            try
            {
                // verify security 
                if ((!new PortalSecurity().InputFilter(e.Text, PortalSecurity.FilterFlag.NoScripting).Equals(e.Text)))
                {
                    throw new SecurityException();

                }
                else if (EditorEnabled == true && this.IsEditable == true && PortalSettings.UserMode == DotNetNuke.Entities.Portals.PortalSettings.Mode.Edit)
                {
                    // get content
                    HtmlTextController objHTML = new HtmlTextController();
                    WorkflowStateController objWorkflow = new WorkflowStateController();
                    HtmlTextInfo objContent = objHTML.GetTopHtmlText(ModuleId, false, WorkflowID);
                    if (objContent == null)
                    {
                        objContent = new HtmlTextInfo();
                        objContent.ItemID = -1;
                    }

                    // set content attributes
                    objContent.ModuleID = ModuleId;
                    objContent.Content = Server.HtmlEncode(e.Text);
                    objContent.WorkflowID = WorkflowID;
                    objContent.StateID = objWorkflow.GetFirstWorkflowStateID(WorkflowID);

                    // save the content
                    objHTML.UpdateHtmlText(objContent, objHTML.GetMaximumVersionHistory(PortalId));
                }
                else
                {
                    throw new SecurityException();
                }
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ModuleAction_Click handles all ModuleAction events raised from the action menu
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        private void ModuleAction_Click(object sender, Entities.Modules.Actions.ActionEventArgs e)
        {
            try
            {
                if (e.Action.CommandArgument == "publish")
                {
                    // verify security 
                    if (this.IsEditable == true && PortalSettings.UserMode == DotNetNuke.Entities.Portals.PortalSettings.Mode.Edit)
                    {
                        // get content
                        HtmlTextController objHTML = new HtmlTextController();
                        HtmlTextInfo objContent = objHTML.GetTopHtmlText(ModuleId, false, WorkflowID);

                        WorkflowStateController objWorkflow = new WorkflowStateController();
                        if (objContent.StateID == objWorkflow.GetFirstWorkflowStateID(WorkflowID))
                        {
                            // publish content
                            objContent.StateID = objWorkflow.GetNextWorkflowStateID(objContent.WorkflowID, objContent.StateID);

                            // save the content
                            objHTML.UpdateHtmlText(objContent, objHTML.GetMaximumVersionHistory(PortalId));

                            // refresh page
                            Response.Redirect(Globals.NavigateURL(), true);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region "Optional Interfaces"

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ModuleActions is an interface property that returns the module actions collection for the module
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {
                // add the Edit Text action
                Entities.Modules.Actions.ModuleActionCollection Actions = new Entities.Modules.Actions.ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString(Entities.Modules.Actions.ModuleActionType.AddContent, LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), false, Security.SecurityAccessLevel.Edit, true, false);

                // get the content
                HtmlTextController objHTML = new HtmlTextController();
                WorkflowStateController objWorkflow = new WorkflowStateController();
				WorkflowID = objHTML.GetWorkflow(ModuleId, TabId, PortalId).Value;

                HtmlTextInfo objContent = objHTML.GetTopHtmlText(ModuleId, false, WorkflowID);
                if ((objContent != null))
                {
                    // if content is in the first state
                    if (objContent.StateID == objWorkflow.GetFirstWorkflowStateID(WorkflowID))
                    {
                        // if not direct publish workflow
                        if (objWorkflow.GetWorkflowStates(WorkflowID).Count > 1)
                        {
                            // add publish action
                            Actions.Add(GetNextActionID(), Localization.GetString("PublishContent.Action", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "publish", "grant.gif", "", true, Security.SecurityAccessLevel.Edit, true, false);
                        }
                    }
                    else
                    {
                        // if the content is not in the last state of the workflow then review is required
                        if (objContent.StateID != objWorkflow.GetLastWorkflowStateID(WorkflowID))
                        {
                            // if the user has permissions to review the content
                            if (WorkflowStatePermissionController.HasWorkflowStatePermission(WorkflowStatePermissionController.GetWorkflowStatePermissions(objContent.StateID), "REVIEW"))
                            {
                                // add approve and reject actions
                                Actions.Add(GetNextActionID(), Localization.GetString("ApproveContent.Action", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "grant.gif", EditUrl("action", "approve", "Review"), false, Security.SecurityAccessLevel.Edit, true, false);
                                Actions.Add(GetNextActionID(), Localization.GetString("RejectContent.Action", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "deny.gif", EditUrl("action", "reject", "Review"), false, Security.SecurityAccessLevel.Edit, true, false);
                            }
                        }
                    }
                }

                // add mywork to action menu
                Actions.Add(GetNextActionID(), Localization.GetString("MyWork.Action", LocalResourceFile), "MyWork.Action", "", "view.gif", EditUrl("MyWork"), false, Security.SecurityAccessLevel.Edit, true, false);

                return Actions;

            }
        }

        #endregion

    }

}

